# -*- coding: utf-8 -*-
import scrapy
from dangdang.items import DangdangItem

class DdSpider(scrapy.Spider):
    name = 'dd'
    allowed_domains = ['search.dangdang.com']
    start_urls = ['http://search.dangdang.com/?key=python&act=input&page_index=1']
    p=1
    def parse(self, response):
        print('正在爬取第'+str(self.p)+'页')
        books = response.css('ul.bigimg li')
        for book in books:
            name=book.xpath('a[1]/@title').extract_first()
            title = book.css('p.name a::attr(title)').extract_first()
            img = book.xpath('a[1]/img/@data-original').extract_first()
            detail = book.css('p.detail::text').extract_first()
            price= book.css('p.price span.search_now_price::text').extract_first()
            author = book.css('p.search_book_author span:nth-child(1) a:nth-child(1)::text').extract_first()
            time = book.css('p.search_book_author span:nth-child(2)::text').extract_first()
            publish = book.css('p.search_book_author span:nth-child(3) a::text').extract_first()

            if time is not None:
                time = time.replace('/','')
            if img is None:
                img = book.xpath('a[1]/img/@src').extract_first()
            item = DangdangItem()
            item['name']=name
            item['title']=title
            item['detail']=detail
            item['price']=price
            item['author']=author
            item['time']=time
            item['publish']=publish
            item['img'] = img
            yield item
        if len(books) == 1:
            self.p += 1
            newurl='http://search.dangdang.com/?key=python&act=input&page_index='+str(self.p)
            yield scrapy.Request(url=newurl,callback=self.parse)
